package com.jiantong.work.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.terminus.common.utils.JsonMapper;
import lombok.Data;
import org.assertj.core.util.Strings;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 佩标管理表
 * Created by mz03 on 2017/7/20.
 */
@Data
public class MarkWearManagement implements Serializable{


    private static final long serialVersionUID = 7724178511508002183L;
    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();
    /**
     * 主键id
     */
    private Long id;

    /**
     * 批号: 本次佩戴的家禽批号 (外键)
     */
    private String animalBatch;

    /**
     * 栏舍号
     */
    private String stableNumber;

    /**
     * 标识类型 比如1、猪 。。。。
     */
    private Integer markType;

    /**
     * 养殖场id
     */
    private Long breedFactoryId;

    /**
     * 养殖场名称
     */
    private String breedFactoryName;


    /**
     * 状态,1: 未提交; 2: 生效.
     */
    private Integer status;


    /**
     * 单位: 1,头 2只
     */
    private Integer unit;

    /**
     * 日龄,规模场佩戴耳标时,这一批家禽的年龄(单位是天)
     */
    private Integer ageDay;

    /**
     * 佩标时间
     */
    private Date woreAt;

    /**
     * 耳标号list
     */
    private List<String> animalNumberList;


    /**
     * 耳标号 存数据库
     */
    @JsonIgnore
    private String animalNumbers;


    public void setAnimalNumbers(String animalNumbers){
        this.animalNumbers = animalNumbers;

        if(Strings.isNullOrEmpty(animalNumbers)){
            this.animalNumberList = new ArrayList<>();
        }else {
            try {
                this.animalNumberList =
                        objectMapper.readValue(animalNumbers, new TypeReference<List<String>>() {});
            } catch (IOException e) {
                //this is fuck exception
            }
        }
    }


    public void setAnimalNumberList(List<String> animalNumberList){
        this.animalNumberList = animalNumberList;

        if(animalNumberList == null || animalNumberList.isEmpty()){
            this.animalNumbers = null;
        }else {
            try {
                this.animalNumbers =
                        objectMapper.writeValueAsString(animalNumberList);
            } catch (JsonProcessingException e) {
                //this is fuck exception
            }
        }
    }


    /**
     * 创建时间
     */
    private Date createdAt;

    /**
     * 更新时间
     */
    private Date updatedAt;

}
